﻿using System;
using System.Drawing;
using System.Windows.Forms;

namespace RSPJ.Windows
{
    public partial class SelectBands : Form
    {
        public string RFile, GFile, BFile, File1, File2, FileBand;
        public int RBand, GBand, BBand, Band1, Band2, Band;

        private void rbtnBand1_CheckedChanged(object sender, EventArgs e)
        {
            if (rbtnBand1.Checked)
                rbtnBand2.Checked = false;
            else
                rbtnBand2.Checked = true;
        }

        private void rbtnBand2_CheckedChanged(object sender, EventArgs e)
        {
            rbtnBand1_CheckedChanged(sender, e);
        }

        private Point RP, GP, BP, P1, P2, P;
        public int lines, samples;
        private int flag;

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (flag == 3)
            {
                if (txtR.Text != "" && txtG.Text != "" && txtB.Text != "")
                {
                    if (RP == GP && GP == BP)
                    {
                        lines = RP.X; samples = RP.Y;
                        DialogResult = DialogResult.OK;
                        Close();
                    }
                    else
                    {
                        MessageBox.Show("请选择行列数相同的波段！", "警告信息", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    }
                }
            }
            else if(flag==2)
            {
                if (txt1.Text != "" && txt2.Text != "")
                {
                    if (P1 == P2)
                    {
                        lines = P1.X; samples = P1.Y;
                        DialogResult = DialogResult.OK;
                        Close();
                    }
                    else
                    {
                        MessageBox.Show("请选择行列数相同的波段！", "警告信息", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    }
                }
            }
            else if(flag==1)
            {
                if (txtBand.Text != "")
                {
                    lines = P.X;samples = P.Y;
                    DialogResult = DialogResult.OK;
                    Close();
                }
                else
                {
                    MessageBox.Show("请选择波段！", "警告信息", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
            }
        }

        private void SelectBands_Shown(object sender, EventArgs e)
        {
            treeBands.Nodes.Clear();
            for (int i = 0; i < GlobalData.fileList.Count; i++)
            {
                TreeNode node = new TreeNode(GlobalData.fileList[i].Name);
                treeBands.Nodes.Add(node);
                for (int j = 1; j <= GlobalData.fileList[i].Head.Bands; j++)
                {

                    TreeNode lnode = new TreeNode("Band " + j);
                    node.Nodes.Add(lnode);
                }
            }
            treeBands.ExpandAll();
            treeBands.SelectedNode = treeBands.Nodes[0];

            if (flag == 1)
            {
                pnl1.Enabled = true;
                pnl1.Visible = true;
                pnl2.Enabled = false;
                pnl2.Visible = false;
                pnl3.Enabled = false;
                pnl3.Visible = false;
                pnl1.BringToFront();
            }
            else if (flag == 2)
            {
                pnl1.Enabled = false;
                pnl1.Visible = false;
                pnl2.Enabled = true;
                pnl2.Visible = true;
                pnl3.Enabled = false;
                pnl3.Visible = false;
                pnl2.BringToFront();
            }
            else if (flag == 3)
            {
                pnl1.Enabled = false;
                pnl1.Visible = false;
                pnl2.Enabled = false;
                pnl2.Visible = false;
                pnl3.Enabled = true;
                pnl3.Visible = true;
                pnl3.BringToFront();
            }

            treeBands.Focus();

        }

        public SelectBands(int flag)
        {
            InitializeComponent();
            this.flag = flag;
        }

        public SelectBands(string title,int flag)
        {
            InitializeComponent();
            Text = title;
            this.flag = flag;
        }

        private void rbtnR_CheckedChanged(object sender, EventArgs e)
        {
            if (rbtnR.Checked)
            {
                rbtnG.Checked = false;
                rbtnB.Checked = false;
            }
            else if (rbtnG.Checked)
            {
                rbtnR.Checked = false;
                rbtnB.Checked = false;
            }
            else
            {
                rbtnR.Checked = false;
                rbtnG.Checked = false;
            }
        }

        private void rbtnG_CheckedChanged(object sender, EventArgs e)
        {
            rbtnR_CheckedChanged(sender, e);
        }

        private void rbtnB_CheckedChanged(object sender, EventArgs e)
        {
            rbtnR_CheckedChanged(sender, e);
        }

        private void treeFile_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (treeBands.SelectedNode.Level == 1)
            {
                treeBands.PathSeparator = " ";
                int a = treeBands.SelectedNode.Level;
                string[] tmp = treeBands.SelectedNode.FullPath.Split(' ');
                if (flag == 3)
                {
                    if (rbtnR.Checked)
                    {
                        RFile = tmp[0];
                        RBand = Convert.ToInt32(tmp[2]);
                        txtR.Text = "Band " + RBand + ":" + RFile;
                        for (int i = 0; i < GlobalData.fileList.Count; i++)
                        {
                            if (RFile == GlobalData.fileList[i].Name)
                            {
                                RP.X = GlobalData.fileList[i].Head.Lines;
                                RP.Y = GlobalData.fileList[i].Head.Samples;
                            }
                        }

                        rbtnG.Checked = true;
                    }
                    else if (rbtnG.Checked)
                    {
                        GFile = tmp[0];
                        GBand = Convert.ToInt32(tmp[2]);
                        txtG.Text = "Band " + GBand + ":" + GFile;
                        for (int i = 0; i < GlobalData.fileList.Count; i++)
                        {
                            if (GFile == GlobalData.fileList[i].Name)
                            {
                                GP.X = GlobalData.fileList[i].Head.Lines;
                                GP.Y = GlobalData.fileList[i].Head.Samples;
                            }
                        }
                        rbtnB.Checked = true;
                    }
                    else
                    {
                        BFile = tmp[0];
                        BBand = Convert.ToInt32(tmp[2]);
                        txtB.Text = "Band " + BBand + ":" + BFile;
                        for (int i = 0; i < GlobalData.fileList.Count; i++)
                        {
                            if (BFile == GlobalData.fileList[i].Name)
                            {
                                BP.X = GlobalData.fileList[i].Head.Lines;
                                BP.Y = GlobalData.fileList[i].Head.Samples;
                            }
                        }
                        rbtnR.Checked = true;
                    }
                }
                else if (flag == 2)
                {
                    if (rbtnBand1.Checked)
                    {
                        File1 = tmp[0];
                        Band1 = Convert.ToInt32(tmp[2]);
                        txt1.Text = "Band " + Band1 + ":" + File1;
                        for (int i = 0; i < GlobalData.fileList.Count; i++)
                        {
                            if (File1 == GlobalData.fileList[i].Name)
                            {
                                P1.X = GlobalData.fileList[i].Head.Lines;
                                P1.Y = GlobalData.fileList[i].Head.Samples;
                            }
                        }
                        rbtnBand2.Checked = true;
                    }
                    else
                    {
                        File2 = tmp[0];
                        Band2 = Convert.ToInt32(tmp[2]);
                        txt2.Text = "Band " + Band2 + ":" + File2;
                        for (int i = 0; i < GlobalData.fileList.Count; i++)
                        {
                            if (File2 == GlobalData.fileList[i].Name)
                            {
                                P2.X = GlobalData.fileList[i].Head.Lines;
                                P2.Y = GlobalData.fileList[i].Head.Samples;
                            }
                        }
                        rbtnBand1.Checked = true;
                    }
                }
                else if(flag == 1)
                {
                    FileBand = tmp[0];
                    Band = Convert.ToInt32(tmp[2]);
                    txtBand.Text = "Band " + Band + ":" + FileBand;
                    for (int i = 0; i < GlobalData.fileList.Count; i++)
                    {
                        if (FileBand == GlobalData.fileList[i].Name)
                        {
                            P.X = GlobalData.fileList[i].Head.Lines;
                            P.Y = GlobalData.fileList[i].Head.Samples;
                        }
                    }
                }
            }
        }
    }
}
